package com.example.rmation.controller;

import com.example.rmation.model.entity.User;
import com.example.rmation.model.request.AddMessageRequest;
import com.example.rmation.model.request.AddUserRequest;
import com.example.rmation.model.request.UpdateMessageRequest;
import com.example.rmation.model.request.UpdateUserRequest;
import com.example.rmation.service.MessageService;
import com.example.rmation.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    @PostMapping("/list")
    public Result messageAllList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, HttpSession session){
        //读取session中是否有用户信息
        User currentUser = (User) session.getAttribute("user");
        //如果没有 返回用户未登录信息
        if (currentUser == null) {
            return Result.error(5000,"您还没有登录");
        }

        PageInfo pageInfo = messageService.messageAll(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @PostMapping("/update")
    public Result studentMessageUpdate(@Valid @RequestBody UpdateMessageRequest updateMessageRequest, HttpSession session){
        User currentUser = (User) session.getAttribute("user");
        //如果没有 返回用户未登录信息
        if (currentUser == null) {
            return Result.error(5000,"您还没有登录");
        }
        //只允许老师和学生本人修改上传信息
        Integer role = currentUser.getRole();
        Integer id = currentUser.getId();
        if (role == 2 || updateMessageRequest.getSid() == id) {
            messageService.studentMessageUpdate(updateMessageRequest);
            return Result.success();
        }else {
            return Result.error("不是本人或教师,无修改权限!");
        }
    }

    @PostMapping("/add")
    public Result messageAdd(@Valid @RequestBody AddMessageRequest addMessageRequest, HttpSession session){
        //读取session中是否有用户信息
        User currentUser = (User) session.getAttribute("user");
        //如果没有 返回用户未登录信息
        if (currentUser == null) {
            return Result.error(5000,"您还没有登录");
        }
        Integer sid = currentUser.getId();
        addMessageRequest.setSid(sid);
        messageService.addMessage(addMessageRequest);
        return Result.success();
    }

    @PostMapping("/listLike")
    public Result messageAllLike(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String name, HttpSession session){
        //读取session中是否有用户信息
        User currentUser = (User) session.getAttribute("user");
        //如果没有 返回用户未登录信息
        if (currentUser == null) {
            return Result.error(5000,"您还没有登录");
        }

        PageInfo pageInfo = messageService.studentMessageLike(pageNum, pageSize,name);

        return Result.success(pageInfo);
    }

    @PostMapping("/listOneself")
    public Result messageAllOneself(@RequestParam Integer pageNum, @RequestParam Integer pageSize, HttpSession session){
        //读取session中是否有用户信息
        User currentUser = (User) session.getAttribute("user");
        //如果没有 返回用户未登录信息
        if (currentUser == null) {
            return Result.error(5000,"您还没有登录");
        }
        String username = currentUser.getUsername();
        PageInfo pageInfo = messageService.messageOneself(pageNum, pageSize,username);

        return Result.success(pageInfo);
    }
}
